<!--  -->
<script setup>
import { ref, computed, watch, reactive, onMounted } from "vue";
import { NBreadcrumb, NBreadcrumbItem, NSkeleton } from "naive-ui";
import { useRoute } from "vue-router";
import store from "@/store";
import { findSubCategory, findSubCategoryGoods } from "@/api/category";

const route = useRoute();
// 面包屑数据
const category = computed(() => {
  const cate = {};
  // 完成获取数据的逻辑
  store.state.category.list.forEach((el) => {
    if (el.children) {
      const sub = el.children.find((sub) => sub.id == route.params.id);
      if (sub) {
        cate.top = { id: el.id, name: el.name };
        cate.sub = { id: sub.id, name: sub.name };
      }
    }
  });
  return cate;
});

let subFilter = ref(null);

// 是否加载数据·
let loading = ref(false);
let finished = ref(false);
const goodsList = ref([]);
let reqParams = reactive({
  page: 1,
  pageSize: 20,
});

// 获取数据函数
const getData = () => {
  loading.value = true;
  reqParams.categoryId = route.params.id;
  findSubCategoryGoods(reqParams).then((res) => {
    if (res.items.length) {
      goodsList.value.push(...res.items);
      reqParams.page++;
    } else {
      // 加载完毕
      finished.value = true;
    }
    // 请求结束
    loading.value = false;
  });
};

// 监听路由变化 发送请求
watch(
  () => route.params.id,
  (newVal) => {
    if (newVal && `/category/sub/${newVal}` === route.path) {
      // 获取分类数据
      findSubCategory(route.params.id).then((res) => {
        res.brands.unshift({ id: null, name: "全部" });
        res.saleProperties.forEach((item) => {
          item.properties.selected = null;
          item.properties.unshift({ id: null, name: "全部" });
        });
        res.brands.selected = null;
        subFilter.value = res;
      });
      goodsList.value = [];
      reqParams = {
        page: 1,
        pageSize: 20,
      };
      finished.value = false;
      getData();
    }
  },
  { immediate: true }
);

const changeLoading = () => {
  getData();
};

const changeFilter = (params) => {
  finished.value = false;
  goodsList.value = [];
  reqParams = {
    ...reqParams,
    ...params,
  };
  reqParams.page = 1;
  getData();
};

const sortChange = (params) => {
  finished.value = false;
  goodsList.value = [];
  reqParams = {
    ...reqParams,
    ...params,
  };
  reqParams.page = 1;
  getData();
};
</script>

<template>
  <div class="bg">
    <div class="container">
      <!-- 面包屑 -->
      <div>
        <n-breadcrumb style="padding: 20px 0px" v-if="category">
          <n-breadcrumb-item href="/">首页</n-breadcrumb-item>
          <n-breadcrumb-item :href="'/category/' + category.top.id">{{
            category.top.name
          }}</n-breadcrumb-item>
          <n-breadcrumb-item href="/">{{
            category.sub.name
          }}</n-breadcrumb-item>
        </n-breadcrumb>
        <div v-else>
          <n-skeleton
            text
            height="35px"
            style="margin: 10px 0px; width: 100%"
          />
        </div>
      </div>

      <SubFilter :filter="subFilter" @change-filter="changeFilter"></SubFilter>
      <div class="goods-list">
        <!-- 排序 -->
        <SubSort @sort-change="sortChange"></SubSort>
        <!-- 列表 -->
        <ul>
          <li v-for="item in goodsList" :key="item.id">
            <RouterLink :to="`/product/${item.id}`" class="goods-item">
              <img v-lazy="item.picture" alt />
              <p class="name ellipse">{{ item.name }}</p>
              <p class="desc ellipse">{{ item.desc }}</p>
              <p class="price">&yen;{{ item.price }}</p>
            </RouterLink>
          </li>
          <Loading
            :loading="loading"
            :finished="finished"
            @onloading="changeLoading"
          ></Loading>
        </ul>
      </div>
    </div>
  </div>
</template> 

<style lang='less' scoped>
.goods-item {
  display: block;
  width: 238px;
  padding: 10px;
  text-align: center;
  .hoverShadow();
  img {
    width: 160px;
    height: 160px;
  }
  p {
    padding-top: 10px;
  }
  .name {
    height: 30px;
    line-height: 30px;
    font-size: 16px;
    padding: 0px 20px;
  }
  .desc {
    color: #999;
    height: 25px;
    line-height: 25px;
    padding: 0px 10px;
  }
  .price {
    padding-top: 0px;
    color: @priceColor;
    font-size: 17px;
  }
}
.goods-list {
  background: #fff;
  padding: 0 20px;
  margin-top: 25px;
  ul {
    display: flex;
    flex-wrap: wrap;
    padding: 0 5px;
    li {
      &:nth-child(5n) {
        margin-right: 0;
      }
    }
  }
}
</style>